<?php
class IndexController extends Controller{
	public function __init(){

		//有锁文件Lock.php不能执行安装
		if(is_file(MODULE_PATH.'lock.php')){
			$this->display();
		}
	}
	//版权信息页面
	public function install(){
		$this->display();
	}
	//数据库连接页面
	public function setDb(){
		$this->display();
	}
	//安装数据
	public function insertSql(){
		$status = @mysql_connect($_POST['DB_HOST'],$_POST['DB_USER'],$_POST['DB_PASSWORD']);
		if(!$status)$this->error('主机无法连接..','setDb');
		if(!mysql_select_db($_POST['DB_DATABASE'])){
			$this->error('数据库连接失败','setDb');
		}
		//更新C函数记录的网站配置项
		C($_POST);
		$db=M();
		$db_prefix='xn_';
		//执行创建表结构的sql
	    require MODULE_PATH.'Sql/structure.php';
	    // 导入数据表
	    $files = Dir::tree(MODULE_PATH.'Sql');
	    foreach($files as $f){
			if(substr($f['filename'],0,3)=='xn_'){
				require $f['path'];
			}
		}
	    //更新管理员信息
	    $code=md5(mt_rand(1,1000).time());
	    $data['uid']=1;
	    $data['username']=$_POST['username'];
	    $data['code']=$code;
	    $data['password']=md5($_POST['password'].$code);
	    $db->table('admin')->save($data);

	    //更新配置文件db.inc.php
	    $data="<?php return ".var_export($_POST,true)."; \n?>";
	    file_put_contents(APP_CONFIG_PATH.'db.inc.php', $data);
	    go('finish');

	}
	//完成
	public function finish(){
		$this->display();
		//创建锁文件lock.php
		touch(MODULE_PATH.'lock.php');
	}
}

?>